package com.googlecode.rjm.util;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {

	/**
	 * Compare two given dates to see whether they are the same date (day, month and year).
	 * 
	 * @param date1 - First date.
	 * @param date2 - Second date.
	 * @return <b>true</b> if they are the same, <b>false</b> if not.
	 */
	public static boolean isSameDate(Date date1, Date date2){
		
		Calendar c = new GregorianCalendar();
		c.setTime(date1);
		int day1 = c.get(Calendar.DAY_OF_MONTH);
		int month1 = c.get(Calendar.MONTH);
		int year1 = c.get(Calendar.YEAR);
		
		c.setTime(date2);
		int day2 = c.get(Calendar.DAY_OF_MONTH);
		int month2 = c.get(Calendar.MONTH);
		int year2 = c.get(Calendar.YEAR);
		
		return day1 == day2 && month1 == month2 && year1 == year2; 
	}
	
	/**
	 * Check whether first date is less than or equal to the second one (comparing just days, months and years). 
	 * 
	 * @param date1 - First date.
	 * @param date2 - Second date.
	 * @return <b>true</b> if first date is less than or equal to the second one, <b>false</b> if not.
	 */
	public static boolean lessOrEqual(Date date1, Date date2){
		return date1.before(date2) || isSameDate(date1, date2);
	}
	
	/**
	 * Check whether first date is less then the second one (comparing just days, months and years). 
	 * 
	 * @param date1 - First date.
	 * @param date2 - Second date.
	 * @return <b>true</b> if first date is less than the second one, <b>false</b> if not.
	 */
	public static boolean less(Date date1, Date date2){
		return date1.before(date2) && !isSameDate(date1, date2);
	}
	
	/**
	 * Check whether first date is greater than or equal to the second one (comparing just days, months and years). 
	 * 
	 * @param date1 - First date.
	 * @param date2 - Second date.
	 * @return <b>true</b> if first date is greater than or equal to the second one, <b>false</b> if not.
	 */
	public static boolean greaterOrEqual(Date date1, Date date2){
		return date1.after(date2) || isSameDate(date1, date2);
	}
	
	/**
	 * Check whether first date is greater then the second one (comparing just days, months and years). 
	 * 
	 * @param date1 - First date.
	 * @param date2 - Second date.
	 * @return <b>true</b> if first date is greater than the second one, <b>false</b> if not.
	 */
	public static boolean greater(Date date1, Date date2){
		return date1.after(date2) && !isSameDate(date1, date2);
	}
	
}
